//导入题库的数据模型

const { T_exam_target } = require('../models/t_exam_target');
const { T_exam_target_to_question } = require('../models/t_exam_target_to_question')



//传输subid查找标签名
exports.getALLPaperTQ = async (req, res) => {
    const subId = req.query.subId;
    const paperType = req.query.paperType;

    try {
        const T_info = await T_exam_target_to_question.findAll({
            where: {
                subId: subId,
                paperType: paperType
            }

        });//根据id查询
        if (T_info) {
            res.json({
                "code": 200,
                "msg": "查询成功",
                "data": T_info,
            });
        } else {
            res.json({
                "code": 404,
                "mag": "查询失败,该题库中的题目id不存在",
            })
        };
    } catch (error) {
        console.log(error.message);
        res.json({
            "code": 500,
            "msg": error.message
        });
    }
};



//插入标签
exports.create_paperTQ = async (req, res) => {
    const body = req.body;
    try {
        const T_info = await T_exam_target_to_question.create({
            //题目信息
            questionType: body.questionType,
            questionNum: body.questionNum,
            score: body.score,
            //标记信息
            paperType: body.paperType,
            subId: body.subId,
            targetId: body.targetId
        });

        res.json({
            "code": 200,
            "msg": '创建成功',
            "data": T_info,
        });
    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        });
    }
};

//更新题库中的标签信息
exports.update_PaperTQ = async (req, res) => {
    const body = req.body

    try {

        await T_exam_target_to_question.update(
            {
                questionNum: body.questionNum,
                questionType: body.questionType,
                score: body.score
            },
            {
                where: {
                    id: body.id,
                }
            },
        );

        res.json({
            "code": 1,
            "msg": "更新成功",
        });

    }
    catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        })
    }
}

//删除文章
exports.delete_paperTQ = async (req, res) => {
    const id = req.query.id;
    try {
        const deletedRows = await T_exam_target_to_question.destroy({
            where: { id },
        });
        if (deletedRows > 0) {
            res.json({
                "code": 1,
                "msg": '删除成功',
            });
        } else {
            res.json({
                "code": 404,
                "msg": `不存在该科目`,
            });
        }
    } catch (error) {
        res.json({
            "code": 500,
            "msg": error.message,
        });
    }
};



exports.getTypeAndNum = async (req, res) => {
    try {
        const ACSId = req.query.ACSId
        const paperType = req.query.paperType
        const mes = await T_exam_target_to_question.findAll({
            where: {
                t_user_and_a_c_s_id: ACSId,
                paperType
            },
        });

        let obj = {}
        for (let i = 0; i < mes.length; i++) {
            if (!obj[mes[i].questionType]) {
                obj[mes[i].questionType] = 0
            }
            obj[mes[i].questionType] += mes[i].score
        }

        res.json({
            "code": 200,
            "data": obj
        })
    } catch (error) {
        res.json({
            "code": 500,
            "data": error.msg
        })
    }

}

exports.targetAndType = async (req, res) => {

    try {
        const ACSId = req.query.ACSId
        const paperType = req.query.paperType

        const mes = await T_exam_target_to_question.findAll({
            where: {
                t_user_and_a_c_s_id: ACSId,
                paperType: paperType
            },
            include: [{
                model: T_exam_target,
                attributes: ['target_name'],
                as: "target"
            }]
        });

        const questionType = {}
        for (let i = 0; i < mes.length; i++) {
            if (!questionType[mes[i].questionType]) {
                questionType[mes[i].questionType] = []
            }
        }

        const TypesArray = Object.keys(questionType)


        const obj = {}
        for (let i = 0; i < mes.length; i++) {
            if (!obj[mes[i].target.target_name]) {
                obj[mes[i].target.target_name] = {}
                for (let j = 0; j < TypesArray.length; j++) {
                    obj[mes[i].target.target_name][TypesArray[j]] = 0
                }

            }

            obj[mes[i].target.target_name][mes[i].questionType] += mes[i].score
        }

        res.json({
            "code": 200,
            "data": obj
        })
    } catch (error) {
        res.json({
            "code": 500,
            "data": error.msg
        })
    }




}